import request from '../../../utils/request'
Page({
        data: {
                mode: {},
                phone: '',
                code: '',
                password: '',
                agree: false
        },
        async onLoad() {
                await this.getmoban()
        },
        getmoban() {
                let t = this
                return new Promise((resolve, reject) => {
                        request({
                                url: 'system/user/a',
                                method: 'get',

                                data: {
                                        userId: 104
                                },
                                success(res) {
                                        if (res.data.code == 200) {
                                                console.log('模版->', res.data.data)
                                                let m = res.data.data
                                                m.roleIds = [100]
                                                t.setData({
                                                        mode: m
                                                }, () => {
                                                        resolve(1)
                                                })
                                        } else {
                                                console.log('模版出错->', res);

                                        }

                                }
                        })
                })
        },
        formSubmit: function (e) {
                let t = this
                const phone = e.detail.value.phone;
                const password = e.detail.value.password;
                const nickname = e.detail.value.nickname;
                let m = t.data.mode
                m.userId = undefined
                m.userName = phone
                m.nickName = nickname
                m.password = password
                m.phonenumber = phone
                m.email = undefined
                console.log('注册数据->', m);
                if (phone && password && nickname) {
                        request({
                                url: 'system/user/add',
                                method: 'post',
                                data: m,
                                success(res) {
                                        console.log('注册回调->', res);
                                        request({
                                                url: 'resident/resident/add',
                                                method: 'post',
                                                data: {
                                                        name: nickname,
                                                        sysUserId: res.data.data
                                                },
                                                success(ress) {
                                                        console.log('注册成功毁掉->', ress);
                                                        wx.navigateTo({
                                                                url: '/pages/login/login',
                                                                success: function (res) {
                                                                        // 通过eventChannel向被打开页面传送数据
                                                                        res.eventChannel.emit('acceptDataFromOpenerPage', {
                                                                                phone: phone,
                                                                                password: password
                                                                        })
                                                                }
                                                        })
                                                },
                                                fail(error) {
                                                        console.log('error->', error);
                                                }
                                        })
                                },
                                fail(error) {
                                        console.log('error->', error);
                                }
                        })
                } else {
                        wx.showToast({
                                title: '请输入完整信息',
                                icon: 'none'
                        });
                }
        }
});